package com.zhan.rkforum.controller.interceptor;

import com.zhan.rkforum.bean.User;
import com.zhan.rkforum.service.DataService;
import com.zhan.rkforum.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DataInterceptor implements HandlerInterceptor {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private DataService dataService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 获取IP地址
        String ip = request.getRemoteHost();
        User user = hostHolder.getUser();
        dataService.recordUV(ip);
        if(user != null){
            dataService.recordDau(user.getId());
        }
        return true;
    }


}
